#include "widget.h"
#include "ui_widget.h"
#include <QMessageBox>
#include <QInputDialog>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_loginButton_clicked()
{
    const QString username = ui->lineEditUsername->text().trimmed();
    const QString password = ui->lineEditPassword->text();

    static QString storedUsername = QStringLiteral("admin");
    static QString storedPassword = QStringLiteral("123456");

    if (username == storedUsername && password == storedPassword) {
        QMessageBox::information(this, tr("登录结果"), tr("登录成功！"));
        // 登录成功后的处理：例如关闭窗口或切换页面
        // close();
    } else {
        QMessageBox::warning(this, tr("登录结果"), tr("用户名或密码错误！"));
    }
}

void Widget::on_registerButton_clicked()
{
    bool ok = false;
    const QString newUsername = QInputDialog::getText(this, tr("注册"), tr("请输入新的用户名："), QLineEdit::Normal, QString(), &ok).trimmed();
    if (!ok) return;
    const QString newPassword = QInputDialog::getText(this, tr("注册"), tr("请输入新的密码："), QLineEdit::Password, QString(), &ok);
    if (!ok) return;

    static QString storedUsername = QStringLiteral("admin");
    static QString storedPassword = QStringLiteral("123456");
    storedUsername = newUsername;
    storedPassword = newPassword;

    QMessageBox::information(this, tr("注册"), tr("注册成功，已更新用户名和密码。"));
}

void Widget::on_cancelButton_clicked()
{
    QApplication::quit();
}

void Widget::on_togglePasswordButton_toggled(bool checked)
{
    ui->lineEditPassword->setEchoMode(checked ? QLineEdit::Normal : QLineEdit::Password);
    ui->togglePasswordButton->setText(checked ? tr("隐藏") : tr("显示"));
}

